<template>
    <!--空白页面-->
    <div v-if="showBlankView" class="form-img">
        <span style="color: #777777">{{this.config.tips}}</span>
    </div>
    <div v-else>
        <FormSolutionComponent :backfillParams="backfillParams" :key="solutionKey" :paramsControl="paramsControl"></FormSolutionComponent>
    </div>
</template>

<script>
/*
表单方案
 */
import FormSolutionComponent from "@/views/modules/form/customForm/FormSolutionShow/FormSolutionComponent"
import FormCustomPublic from "../FormCustomPublic";
import BusEvent from "../BusEvent";
import Util from "@/js/Util.js";
import PublicApi from "@/api/public.js"

export default {
    name: "FormView",
    mixins:[FormCustomPublic,BusEvent],
    components:{
        FormSolutionComponent
    },
    props: {
        config:{
            type:Object,
            default:{}
        },
    },
    data(){
        return{
            //显示空白页面
            showBlankView:true,
            paramsControl:{},
            solutionKey:Util.randomId(),
            backfillParams:{},//回填参数
        }
    },
    created(){
       this.loadForm();
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formCustomViewLang",key);
        },
        loadForm(args){
            if( !this.config || !this.config.alias){
                this.showBlankView=true;
                return ;
            }
            //通过自定义的事件
            if(args && args.type=="custom" && args.params){
                this.paramsControl={
                    alias:this.config.alias,
                    pkId:args.params.pk,
                    action:args.params.action,
                    params:""
                }
                return;
            }
            if(this.config.typeSelection=='backfill'){
                this.showBlankView = false
                this.initBackfill(args)
            }else if( this.config.receive){
                let paramsControl = {
                    alias:this.config.alias,
                    action:'edit',
                    pkId:"",
                    params:""
                }
                let receive=this.config.receive;
                paramsControl.action = receive.isRead && receive.isRead=="YES"?"detail":"edit";
                let mapping = [];
                let formData = {};
                //取路由参数
                let params=this.getRouterParams();
                if (receive.type == 'url' || !receive.type) {
                    if (receive.isPk == 'YES') {
                        mapping = Util.deepClone(receive.pkMapping);
                        if (mapping[0].valueSource == 'param'){
                            if (params[mapping[0].valueDef]) {
                                mapping[0].valueSource = 'fixedVar';
                                mapping[0].valueDef = params[mapping[0].valueDef];
                            } else if ( params[mapping[0].name]) {
                                mapping[0].valueSource = 'fixedVar';
                                mapping[0].valueDef = params[mapping[0].name];
                            }
                        }
                    } else {
                        mapping = Util.deepClone(receive.mapping);
                        for (let i = 0; i < mapping.length; i++) {
                            let o = mapping[i];
                            if (o.valueSource == 'param' && params[o.name] && o.valueDef) {
                                if(receive.isData=='YES'){
                                    formData[o.name]=params[o.name];
                                }
                                o.valueSource = 'fixedVar';
                                o.valueDef = params[o.name];
                            }
                        }
                    }
                } else if (args){
                    if (receive.isPk == 'YES') {
                        mapping = Util.deepClone(receive.pkMapping);
                        if (mapping[0].valueSource == 'param' && args.params[mapping[0].valueDef]) {
                            mapping[0].valueSource = 'fixedVar';
                            mapping[0].valueDef = args.params[mapping[0].valueDef];
                        }
                    } else {
                        mapping = Util.deepClone(receive.mapping);
                        for (let i = 0; i < mapping.length; i++) {
                            let o = mapping[i];
                            if (o.valueSource == 'param' && args.params[o.valueDef]) {
                                if(receive.isData=='YES'){
                                    formData[o.name]=args.params[o.valueDef];
                                }
                                o.valueSource = 'fixedVar';
                                o.valueDef = args.params[o.valueDef];
                            }
                        }
                    }
                }
                let self = this;
                this.getParams(mapping,function(res){
                    if(receive.isPk == 'YES' ){
                        if(res.pk){
                            paramsControl.pkId=res.pk;
                        }else {
                            paramsControl.pkId="";
                        }
                    }
                    paramsControl.params=res;
                    self.showBlankView=false;
                    self.solutionKey=Util.randomId();
                    self.paramsControl = paramsControl;
                })
            }
        },
        /**
         * 处理页面定制公共事件
         * @param args
         */
        handPublicReceive(args){
            //处理自定义方法。
            let method=args.method;
            if(method){
                let data=args.data;
                let rxForm_=this.$refs.rxForm;
                if(!rxForm_){
                    return;
                }
                let func=rxForm_.formVm[method];
                if(!func){
                    this.$message.warn(this.i18n('methodMessage','请在当前组件中配置方法:') +method);
                    return;
                }
                if(data){
                    func(data);
                }
                else{
                    func();
                }
                return;
            }
            this.showBlankView=args.showBlankView ||false;
            if(this.showBlankView){
                return;
            }
            this.loadForm(args);
        },
        initBackfill(args){
            this.paramsControl = {
                alias:this.config.alias,
                action:'edit',
                pkId:"",
                params:""
            }
            let backfill = this.config.backfill
            let mapping = Util.deepClone(backfill.mapping);
            let params=args?args.params:this.getRouterParams();
            let backfillMapping = [];
            mapping.forEach(res=>{
                let valueSourceList = ['fixedVar','script','constantVar']
                backfillMapping.push({
                    valueSource:res.valueSource=='param'?'fixedVar':res.valueSource,
                    name:res.fieldName,
                    valueDef:res.valueSource=='param'?params[res.valueDef]:res.valueDef,
                    idx_:res.idx_
                })
            })
            PublicApi.getParams(JSON.stringify(backfillMapping)).then(res=>{
                this.backfillParams =res
            })
        },
    }
}
</script>

<style scoped>

</style>
